<template>
  <el-container>
    <!-- 头部区域 -->
    <el-header>
      <!-- logo -->
      <div class="avator">
        <img src="~assets/hlogo.png" alt="" />
        <span>H-商城后台管理系统</span>
      </div>
      <el-button type="info" @click="loginOut">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏菜单 -->
      <el-aside :width="isFold ? '60px' : '200px'">
        <!-- 折叠 -->
        <div class="fold" @click="isFold = !isFold">| | |</div>
        <el-menu :default-active="defaultActive" class="el-menu-vertical-demo" background-color="#001529" text-color="#fff"
         active-text-color="#ffd04b" :collapse="isFold" :unique-opened="true" router>
          <el-submenu :index="'/' + menu.path" v-for="menu in menus" :key="menu.id">
            <template slot="title">
              <i :class="menuIcons[menu.id]"></i>
              <span>{{menu.authName}}</span>
            </template>
            <el-menu-item :index="'/' + item.path" v-for="item in menu.children" :key="item.id" @click="changeActive('/'+item.path)">
              <i class="el-icon-menu"></i>
              <span slot="title">{{item.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体内容 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
// import { mapState } from 'vuex'
export default {
  data() {
    return {
      menus: [],
      menuIcons: {
        125: 'el-icon-user-solid',
        103: 'el-icon-s-management',
        101: 'el-icon-present',
        102: 'el-icon-document',
        145: 'el-icon-data-line',
      },
      isFold: false,
      defaultActive: '/welcome'
    }
  },
  created() {
    this.getMenus()
    this.defaultActive = window.sessionStorage.getItem('defaultActive')
  },
  computed: {
    // ...mapGetters('menus',{ defaultActive: 'getActive' })
    // ...mapState({
    //   defaultActive: state => state.menus.defaultActive
    // })
  },
  methods: {
    // 退出登录
    loginOut() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取下拉菜单列表
    async getMenus() {
      const { data, meta } = await this.$http.get('menus')
      if (meta.status !== 200) return this.$message.error('获取菜单列表失败！')
      this.menus = data
    },
    changeActive(activePath) {
      // this.$store.commit('menus/setDefaultActive',activePath)
      this.defaultActive = activePath
      window.sessionStorage.setItem('defaultActive',activePath)
    }
  },
}
</script>

<style lang="less" scoped>
.el-header {
  display: flex;
  background-color: #001529;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  color: #fff;
}
.avator {
  display: flex;
  height: 60px;
  align-items: center;
  font-size: 20px;
  font-weight: bold;
  img {
    height: 100%;
    width: auto;
  }
  span {
    margin-left: 15px;
  }
}
.el-aside {
  background-color: #001529;
}
.el-main {
  background-color: #fff;
}
.el-menu {
  border: 0;
}
.fold {
  text-align: center;
  cursor: pointer;
  background-color: #1e2c38;
  color: #fff;
}
</style>
